package com.wangyadong.hobby.schedule.jobtask.dsa.stack;


import org.junit.Assert;

import java.util.Stack;

public class MatchStringDelimiter {

    private static final String opening = "({[";
    private static final String closing = ")}]";
    private static final Stack<Character> stack = new Stack();

    public static boolean isMatch(String text) {
        for (char cha : text.toCharArray()) {
            if (opening.indexOf(cha) != -1) {
                stack.push(cha);
            } else if (closing.indexOf(cha) != -1) {
                if (stack.empty()) {
                    return false;
                }
                Character pop = stack.pop();
                System.out.println("pop is " + pop + " and  input is " + cha);
                if (opening.indexOf(pop) != closing.indexOf(cha)) {
                    return false;
                }
            }

        }

        return stack.empty();


    }

    public static void main(String[] args) {
        Assert.assertTrue(!isMatch("(])]"));
        Assert.assertTrue(isMatch("([])"));
    }
}
